import { useReducer } from "react"

export default function Demo6()
{
    // 用于复杂的state操作逻辑 类似redux中的功能
    const [count,dispatch] = useReducer((state,action) =>
    {
        switch(action.type)
        {
            case 'add':
                state += 1;
                break;

            case 'minus':
                state -= 1;
                break;

            default:
        }

        return state;
    },0);

    return (
        <>
            <div>{count}</div>
            <button onClick={() => {dispatch({type: 'add'})}}>useReducer加</button>
            <button onClick={() => {dispatch({type: 'minus'})}}>useReducer减</button>
        </>
    )
}